package com.sync.storage.controller;

import com.sync.storage.bean.Base64;
import com.sync.storage.bean.Stu;
import com.sync.storage.bean.Student;
import com.sync.storage.client.StuClient;
import com.sync.storage.service.student.IStudentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Encoder;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;

@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StuClient stuClient;

    @Autowired
    private IStudentService studentService;

    @RequestMapping("/upload")
    public void upload(Student student, @RequestParam("files") MultipartFile[] files) throws IOException {
        InputStream inputStream = null;
        ByteArrayOutputStream byteArrayInputStream = null;
        try {
            for (MultipartFile file : files) {
                byte[] buffer=new byte[1024];
                int len=0;
                inputStream = file.getInputStream();
                byteArrayInputStream = new ByteArrayOutputStream();
                while((len=inputStream.read(buffer))!=-1){
                    byteArrayInputStream.write(buffer,0,len);
                }
                byteArrayInputStream.flush();
                byte data[] = byteArrayInputStream.toByteArray();
                String filename = file.getOriginalFilename();
                if (filename.contains("doc")){
                    student.setResume(data);
                }else {
                    student.setPhoto(data);
                }
            }
            //将图片进行入库的操作
            studentService.insert(student);
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    @RequestMapping("/sync")
    public String sync(String name){
        Student student = studentService.selectByPrimaryKey(name);
        Stu stu = new Stu();
        BeanUtils.copyProperties(student,stu);
        String inset = stuClient.inset(stu);
        return inset;
    }


    @RequestMapping("/testBase64")
    public String testBase64(String name){
        Student student = studentService.selectByPrimaryKey(name);
        BASE64Encoder base64Encoder = new BASE64Encoder();
        Base64 base64 = new Base64();
        base64.setAge(student.getAge());
        base64.setName(student.getName());
        base64.setPhoto(base64Encoder.encode(student.getPhoto()));
        base64.setResume(base64Encoder.encode(student.getResume()));
        return stuClient.testBase64(base64);
    }

    @RequestMapping("/downloadImage")
    public void downloadImage(String name, HttpServletResponse response){
        Student student = studentService.selectByPrimaryKey(name);
        String fileName = student.getName();
        try {
            ServletOutputStream outputStream = response.getOutputStream();
            response.setContentType("image/jpg");
            //设置返回文件名的编码格式
            response.setCharacterEncoding("utf-8");
            fileName = URLEncoder.encode(fileName, "utf-8");
            //向输出流写文件
            //写之前设置响应流以附件的形式打开返回值,这样可以保证前边打开文件出错时异常可以返回给前台
            response.setHeader("Content-Disposition","attachment;filename="+fileName+".jpg");
            outputStream.write(student.getPhoto());
            outputStream.flush();
            outputStream.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @RequestMapping("/downloadFile")
    public void downloadFile(String name,HttpServletResponse response){
        Student student = studentService.selectByPrimaryKey(name);
        String fileName = student.getName();
        try {
            ServletOutputStream outputStream = response.getOutputStream();
            //向输出流写文件
            //写之前设置响应流以附件的形式打开返回值,这样可以保证前边打开文件出错时异常可以返回给前台
            response.setContentType("text/x-plain");
            //设置返回文件名的编码格式
            response.setCharacterEncoding("utf-8");
            fileName = URLEncoder.encode(fileName, "utf-8");
            response.setHeader("Content-Disposition","attachment;filename="+fileName+".doc");
            outputStream.write(student.getResume());
            outputStream.flush();
            outputStream.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
